// 新歌首发
const getNewMusic = () => {
  wx.showLoading({
    title: '加载中',
  });
  wx.showNavigationBarLoading();
  return new Promise((resolve, reject) => {
    wx.request({
      url: 'https://m.kugou.com',
      data: {
        json: true
      },
      success: res => {
        let musicList = res.data.data;
        musicList.forEach(i => {
          i.album_sizable_cover = i.album_sizable_cover.replace('{size}/', '');
        })
        resolve(res);
      },
      fail: err => {
        reject(err);
      },
      complete: () => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        wx.hideNavigationBarLoading();
      }
    })
  })
}

// 排行榜
const getRankMusic = () => {
  wx.showLoading({
    title: '加载中',
  });
  wx.showNavigationBarLoading();
  return new Promise((resolve, reject) => {
    wx.request({
      url: 'https://m.kugou.com/rank/list',
      data: {
        json: true
      },
      success: res => {
        let musicList = res.data.rank.list;
        musicList.forEach(i => {
          i.imgurl = i.imgurl.replace('{size}/', '');
        })
        resolve(musicList);
      },
      fail: err => {
        reject(err);
      },
      complete: () => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        wx.hideNavigationBarLoading();
      }
    })
  })
}
// 排行版分类歌曲列表
const rankMusicList = (data) => {
  wx.showLoading({
    title: '加载中',
  });
  wx.showNavigationBarLoading();
  return new Promise((resolve, reject) => {
    wx.request({
      url: 'https://m.kugou.com/rank/info',
      data: data,
      success: res => {
        let songList = res.data.songs.list;
        songList.forEach(i => {
          // i.singername = i.filename.match(/(\S*) - /)[1];
          // i.songname = i.filename.match(/ - (\S*)/)[1];
          i.album_sizable_cover = i.album_sizable_cover.replace('{size}/', '');
        })
        resolve(res);
      },
      fail: err => {
        reject(err);
      },
      complete: () => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        wx.hideNavigationBarLoading();
      }
    })
  })
}

// 歌单
const getPlistMusic = () => {
  wx.showLoading({
    title: '加载中',
  });
  wx.showNavigationBarLoading();
  return new Promise((resolve, reject) => {
    wx.request({
      url: 'https://m.kugou.com/plist/index',
      data: {
        json: true
      },
      success: res => {
        let musicList = res.data.plist.list.info;
        musicList.forEach(i => {
          i.imgurl = i.imgurl.replace('{size}/', '');
        })
        resolve(musicList);
      },
      fail: err => {
        reject(err);
      },
      complete: () => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        wx.hideNavigationBarLoading();
      }
    })
  })
}
// 歌单下的音乐列表
const plistMusicList = (id) => {
  wx.showLoading({
    title: '加载中',
  });
  wx.showNavigationBarLoading();
  return new Promise((resolve, reject) => {
    wx.request({
      url: 'https://m.kugou.com/plist/list/' + id + '/',
      data: {
        json: true
      },
      success: res => {
        let info = res.data.info.list;
        let songList = res.data.list.list.info;
        songList.forEach(i => {
          i.singername = i.filename.match(/(\S*) - /)[1];
          i.songname = i.filename.match(/ - (\S*)/)[1];
        })
        info.imgurl = info.imgurl.replace('{size}/', '');
        resolve(res);
      },
      fail: err => {
        reject(err);
      },
      complete: () => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        wx.hideNavigationBarLoading();
      }
    })
  })
}

// 歌手
const getSingerClass = () => {
  wx.showLoading({
    title: '加载中',
  });
  wx.showNavigationBarLoading();
  return new Promise((resolve, reject) => {
    wx.request({
      url: 'https://m.kugou.com/singer/class',
      data: {
        json: true
      },
      success: res => {
        resolve(res);
      },
      fail: err => {
        reject(err);
      },
      complete: () => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        wx.hideNavigationBarLoading();
      }
    })
  })
}

// 歌手分类下歌手列表
const getSingerList = (id) => {
  wx.showLoading({
    title: '加载中',
  });
  wx.showNavigationBarLoading();
  return new Promise((resolve, reject) => {
    wx.request({
      url: 'https://m.kugou.com/singer/list/' + id,
      data: {
        json: true
      },
      success: res => {
        let singerList = res.data.singers.list.info;
        singerList.forEach(item => {
          item.singer.forEach(i => {
            i.imgurl = i.imgurl.replace('{size}/', '200/');
          })
        })
        resolve(singerList);
      },
      fail: err => {
        reject(err);
      },
      complete: () => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        wx.hideNavigationBarLoading();
      }
    })
  })
}
// 歌手信息
const getSingerInfo = (id) => {
  wx.showLoading({
    title: '加载中',
  });
  wx.showNavigationBarLoading();
  return new Promise((resolve, reject) => {
    wx.request({
      url: 'https://m.kugou.com/singer/info/' + id + '/',
      data: {
        json: true
      },
      success: res => {
        let info = res.data.info;
        info.imgurl = info.imgurl.replace('{size}/', '600/');
        if (info.year_listener >= 10000 && info.year_listener < 100000000) {
          info.year_listener = (info.year_listener / 10000).toFixed(2) + '万'
        } else if (info.year_listener >= 100000000) {
          info.year_listener = (info.year_listener / 100000000).toFixed(2) + '亿'
        }
        resolve(res);
      },
      fail: err => {
        reject(err);
      },
      complete: () => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        wx.hideNavigationBarLoading();
      }
    })
  })
}

// 热门搜索
const getHotSearch = () => {
  wx.showLoading({
    title: '加载中',
  });
  wx.showNavigationBarLoading();
  return new Promise((resolve, reject) => {
    wx.request({
      url: 'https://mobilecdn.kugou.com/api/v3/search/hot',
      data: {
        format: json,
        plat: 0,
        count: 30
      },
      success: res => {
        resolve(res);
      },
      fail: err => {
        reject(err);
      },
      complete: () => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        wx.hideNavigationBarLoading();
      }
    })
  })
}

// 音乐搜索
const searchMusic = (keyword) => {
  wx.showLoading({
    title: '加载中',
  })
  return new Promise((resolve, reject) => {
    wx.request({
      // url: 'https://mobilecdn.kugou.com/api/v3/search/song?format=json&keyword=柯柯柯啊&page=1&pagesize=20&showtype=1',
      url: 'https://mobilecdn.kugou.com/api/v3/search/song',
      data: {
        format: 'JSON',
        keyword,
        page: 1,
        pagesize: 20,
        showtype: 1
      },
      success: res => {
        resolve(res);
      },
      fail: err => {
        reject(err);
      },
      complete: () => {
        wx.hideLoading();
      }
    })
  })
}

// 歌曲详情
const infoMusic = (hash) => {
  wx.showLoading({
    title: '加载中',
  })
  return new Promise((resolve, reject) => {
    wx.request({
      url: 'https://m.kugou.com/app/i/getSongInfo.php',
      data: {
        cmd: 'playInfo',
        hash
      },
      success: res => {
        resolve(res);
      },
      fail: err => {
        reject(err);
      },
      complete: () => {
        wx.hideLoading();
      }
    })
  })
}

module.exports = {
  getNewMusic,
  getRankMusic,
  rankMusicList,
  getPlistMusic,
  plistMusicList,
  getSingerClass,
  getSingerList,
  getSingerInfo,
  getHotSearch,
  searchMusic,
  infoMusic
}